-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix panic risk in func Middleware #6
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your patch YueHonghui.
I'd like to see if we can keep this library and opentracing-contrib/go-stdlib consistent.
c.Next() | ||
defer func() { | ||
ext.HTTPStatusCode.Set(sp, uint16(c.Writer.Status())) | ||
sp.Finish() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When there is is a panic "c.Writer.Status" will be equal to 200.
This change means we will successfully complete the trace (sp.Finish()), but the response code will be 200 -- indicating the operation was successful. I wonder if that could be a little misleading?
Looking at https://github.com/opentracing-contrib/go-stdlib (which this library is derived from), our current behaviour matches theirs. When there is a panic the span is not finished (sp.Finish() is not called).
How would you feel about proposing a change similar to this to opentracing-contrib/go-stdlib:
defer func() {
if recover() != nil {
ext.HTTPStatusCode.Set(
sp, uint16(http.StatusInternalServerError))
} else {
ext.HTTPStatusCode.Set(sp, uint16(sct.status))
}
sp.Finish()
}()
That would mean that they would complete the span with an error if there is a panic. If that (or something similar) was acceptable we could then "backport" the change here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noticed https://github.com/opentracing-contrib/go-stdlib !
tr := &mocktracer.MockTracer{} | ||
mw := Middleware(tr) | ||
r := gin.New() | ||
r.Use(gin.Recovery(), mw) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you reverse this to be
r.Use(mw, gin.Recover())
you can check for a status code of 500 in the panic case and 200 in the non-panic case.
See https://github.com/gin-gonic/gin/blob/master/recovery.go#L68
@stuart-mclaren |
Dears, any progress on the status code? Just FYI, my current fix is just to leave the status code as it is: |
In func Middleware,
c.Next
would be panic, then span.Finish will not be executed.